include $(TOPDIR)/rules.mk

PKG_NAME:=qca-nss-drv
PKG_RELEASE:=15

PKG_SOURCE_URL:=https://git.codelinaro.org/clo/qsdk/oss/lklm/nss-drv.git
PKG_SOURCE_PROTO:=git
PKG_SOURCE_DATE:=2024-04-06
PKG_SOURCE_VERSION:=53a0dc1
PKG_MIRROR_HASH:=9b7774407f950f4ae8390e77e67ecdbeb0af5575191645666a0b5caa6e3bd413
QSDK_VERSION:=12.5
PKG_VERSION:=$(QSDK_VERSION).$(subst -,.,$(PKG_SOURCE_DATE))~$(PKG_SOURCE_VERSION)

PKG_BUILD_PARALLEL:=1
PKG_FLAGS:=nonshared

PKG_CONFIG_DEPENDS:= \
	CONFIG_NSS_DRV_BRIDGE_ENABLE \
	CONFIG_NSS_DRV_CAPWAP_ENABLE \
	CONFIG_NSS_DRV_C2C_ENABLE \
	CONFIG_NSS_DRV_CLMAP_ENABLE \
	CONFIG_NSS_DRV_CRYPTO_ENABLE \
	CONFIG_NSS_DRV_DTLS_ENABLE \
	CONFIG_NSS_DRV_GRE_ENABLE \
	CONFIG_NSS_DRV_GRE_REDIR_ENABLE \
	CONFIG_NSS_DRV_GRE_TUNNEL_ENABLE \
	CONFIG_NSS_DRV_IGS_ENABLE \
	CONFIG_NSS_DRV_IPSEC_ENABLE \
	CONFIG_NSS_DRV_IPV4_REASM_ENABLE \
	CONFIG_NSS_DRV_IPV6_ENABLE \
	CONFIG_NSS_DRV_IPV6_REASM_ENABLE \
	CONFIG_NSS_DRV_LSO_RX_ENABLE \
	CONFIG_NSS_DRV_L2TP_ENABLE \
	CONFIG_NSS_DRV_LAG_ENABLE \
	CONFIG_NSS_DRV_MAPT_ENABLE \
	CONFIG_NSS_DRV_MATCH_ENABLE \
	CONFIG_NSS_DRV_MIRROR_ENABLE \
	CONFIG_NSS_DRV_OAM_ENABLE \
	CONFIG_NSS_DRV_PORTID_ENABLE \
	CONFIG_NSS_DRV_PPPOE_ENABLE \
	CONFIG_NSS_DRV_PPTP_ENABLE \
	CONFIG_NSS_DRV_PVXLAN_ENABLE \
	CONFIG_NSS_DRV_QRFS_ENABLE \
	CONFIG_NSS_DRV_QVPN_ENABLE \
	CONFIG_NSS_DRV_RMNET_ENABLE \
	CONFIG_NSS_DRV_SHAPER_ENABLE \
	CONFIG_NSS_DRV_SJACK_ENABLE \
	CONFIG_NSS_DRV_UDP_ST_ENABLE \
	CONFIG_NSS_DRV_TLS_ENABLE \
	CONFIG_NSS_DRV_TRUSTSEC_ENABLE \
	CONFIG_NSS_DRV_TRUSTSEC_RX_ENABLE \
	CONFIG_NSS_DRV_TSTAMP_ENABLE \
	CONFIG_NSS_DRV_TUN6RD_ENABLE \
	CONFIG_NSS_DRV_TUNIPIP6_ENABLE \
	CONFIG_NSS_DRV_VIRT_IF_ENABLE \
	CONFIG_NSS_DRV_VLAN_ENABLE \
	CONFIG_NSS_DRV_VXLAN_ENABLE \
	CONFIG_NSS_DRV_WIFIOFFLOAD_ENABLE \
	CONFIG_NSS_DRV_WIFI_EXT_VDEV_ENABLE \
	CONFIG_NSS_DRV_WIFI_MESH_ENABLE \
	CONFIG_NSS_DRV_WIFI_LEGACY_ENABLE \
	CONFIG_NSS_FIRMWARE_VERSION_11_4

ifeq ($(CONFIG_NSS_FIRMWARE_VERSION_11_4),y)
PKG_SOURCE_DATE:=2021-09-13
PKG_SOURCE_VERSION:=53e5863
PKG_MIRROR_HASH:=24ffda06f6f4fe52f6d47a32d43d58e111a35ae65a0893664c931cb7bb7eca12
PATCH_DIR:=$(CURDIR)/patches-11.4
QSDK_VERSION:=11.4.0.5
PKG_VERSION:=$(QSDK_VERSION).$(subst -,.,$(PKG_SOURCE_DATE))~$(PKG_SOURCE_VERSION)
endif

include $(INCLUDE_DIR)/kernel.mk
include $(INCLUDE_DIR)/package.mk

NSS_CLIENTS_DIR:=$(TOPDIR)/qca/src/qca-nss-clients

define KernelPackage/qca-nss-drv
  SECTION:=kernel
  CATEGORY:=Kernel modules
  SUBMENU:=Network Devices
  DEPENDS:=@(TARGET_ipq806x||TARGET_qualcommax||TARGET_ipq807x||TARGET_ipq60xx) \
  				 +!TARGET_ipq806x:kmod-qca-nss-dp \
  				 +TARGET_qualcommax_ipq807x:nss-firmware-ipq8074
  TITLE:=Kernel driver for NSS (core driver)
  FILES:=$(PKG_BUILD_DIR)/qca-nss-drv.ko
  AUTOLOAD:=$(call AutoLoad,32,qca-nss-drv)
endef

define KernelPackage/qca-nss-drv/config
	source "$(SOURCE)/Config.in"
endef

define KernelPackage/qca-nss-drv/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/hotplug.d/firmware
	$(INSTALL_DIR) $(1)/usr/bin

	$(INSTALL_BIN) ./files/skb_recycler.init $(1)/etc/init.d/skb_recycler
	$(INSTALL_BIN) ./files/qca-nss-drv.init $(1)/etc/init.d/qca-nss-drv
	$(INSTALL_DATA) ./files/qca-nss-drv.uci $(1)/etc/config/nss
	$(INSTALL_DATA) ./files/qca-nss-drv.hotplug $(1)/etc/hotplug.d/firmware/10-qca-nss-fw
	$(INSTALL_BIN) ./files/qca-nss-drv.debug $(1)/usr/bin/nss_stats
endef

define KernelPackage/qca-nss-drv/conffiles
/etc/config/nss
/etc/config/skb_recycler
endef

define KernelPackage/qca-nss-drv/Description
This package contains a NSS driver for QCA chipset
endef

ifeq ($(CONFIG_TARGET_SUBTARGET), "ipq807x")
  SOC="ipq807x_64"
  subtarget:=$(CONFIG_TARGET_SUBTARGET)
else ifeq ($(CONFIG_TARGET_SUBTARGET), "ipq60xx")
  SOC="ipq60xx_64"
  subtarget:=$(CONFIG_TARGET_SUBTARGET)
else ifeq ($(CONFIG_TARGET_SUBTARGET), "ipq50xx")
  SOC="ipq50xx_64"
  subtarget:=$(CONFIG_TARGET_SUBTARGET)
endif

define Build/InstallDev
	mkdir -p $(1)/usr/include/qca-nss-drv
	$(CP) $(PKG_BUILD_DIR)/exports/* $(1)/usr/include/qca-nss-drv/
ifneq (, $(findstring $(subtarget), "ipq807x" "ipq807x_64" "ipq60xx" "ipq60xx_64" "ipq50xx" "ipq50xx_64"))
	$(RM) $(1)/usr/include/qca-nss-drv/nss_ipsecmgr.h
endif
endef

EXTRA_CFLAGS+= -I$(STAGING_DIR)/usr/include/qca-nss-dp \
			   -I$(STAGING_DIR)/usr/include/qca-ssdk \
			   -Wno-unused-variable -Wno-enum-conversion

ifdef CONFIG_NSS_MEM_PROFILE_MEDIUM
  EXTRA_CFLAGS += -DNSS_MEM_PROFILE_MEDIUM
else ifdef CONFIG_NSS_MEM_PROFILE_LOW
  EXTRA_CFLAGS += -DNSS_MEM_PROFILE_LOW
endif

ifdef CONFIG_NSS_FIRMWARE_VERSION_12_5
  EXTRA_CFLAGS += -DNSS_FIRMWARE_VERSION_12_5
endif

DRV_MAKE_OPTS:=

ifndef CONFIG_NSS_DRV_BRIDGE_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_BRIDGE_ENABLE=n
endif

ifneq ($(CONFIG_TARGET_BOARD), "ipq806x")
ifndef CONFIG_NSS_DRV_C2C_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_C2C_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_CLMAP_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_CLMAP_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_CRYPTO_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_CRYPTO_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_DTLS_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_DTLS_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_GRE_ENABLE
ifndef CONFIG_PACKAGE_kmod-gre
   DRV_MAKE_OPTS += NSS_DRV_GRE_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_GRE_REDIR_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_GRE_REDIR_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_GRE_TUNNEL_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_GRE_TUNNEL_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_IGS_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_IGS_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_IPSEC_ENABLE
ifndef CONFIG_PACKAGE_kmod-ipsec
   DRV_MAKE_OPTS += NSS_DRV_IPSEC_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_IPV4_REASM_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_IPV4_REASM_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_IPV6_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_IPV6_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_IPV6_REASM_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_IPV6_REASM_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_L2TP_ENABLE
ifndef CONFIG_PACKAGE_kmod-l2tp
   DRV_MAKE_OPTS += NSS_DRV_L2TP_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_LAG_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_LAG_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_MAPT_ENABLE
ifndef CONFIG_PACKAGE_kmod-nat46
   DRV_MAKE_OPTS += NSS_DRV_MAPT_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_MATCH_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_MATCH_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_MIRROR_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_MIRROR_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_OAM_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_OAM_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_PORTID_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_PORTID_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_PPPOE_ENABLE
ifndef CONFIG_PACKAGE_kmod-pppoe
   DRV_MAKE_OPTS += NSS_DRV_PPPOE_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_PPTP_ENABLE
ifndef CONFIG_PACKAGE_kmod-pptp
   DRV_MAKE_OPTS += NSS_DRV_PPTP_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_PVXLAN_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_PVXLAN_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_QRFS_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_QRFS_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_QVPN_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_QVPN_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_OVPN_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_OVPN_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_RMNET_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_RMNET_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_SHAPER_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_SHAPER_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_SJACK_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_SJACK_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_TLS_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_TLS_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_TRUSTSEC_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_TRUSTSEC_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_TRUSTSEC_RX_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_TRUSTSEC_RX_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_TSTAMP_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_TSTAMP_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_TUN6RD_ENABLE
ifndef CONFIG_PACKAGE_kmod-sit
   DRV_MAKE_OPTS += NSS_DRV_TUN6RD_ENABLE=n
endif
endif

ifndef CONFIG_NSS_DRV_TUNIPIP6_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_TUNIPIP6_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_VIRT_IF_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_VIRT_IF_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_VLAN_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_VLAN_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_VXLAN_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_VXLAN_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_WIFIOFFLOAD_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_WIFIOFFLOAD_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_WIFI_EXT_VDEV_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_WIFI_EXT_VDEV_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_WIFI_MESH_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_WIFI_MESH_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_UDP_ST_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_UDP_ST_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_CAPWAP_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_CAPWAP_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_LSO_RX_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_LSO_RX_ENABLE=n
endif

ifndef CONFIG_NSS_DRV_WIFI_LEGACY_ENABLE
   DRV_MAKE_OPTS += NSS_DRV_WIFI_LEGACY_ENABLE=n
endif

define Build/Configure
	$(LN) arch/nss_$(SOC).h $(PKG_BUILD_DIR)/exports/nss_arch.h
endef

define Build/Compile
	+$(MAKE) -C "$(LINUX_DIR)" $(strip $(DRV_MAKE_OPTS)) \
		CROSS_COMPILE="$(TARGET_CROSS)" \
		ARCH="$(LINUX_KARCH)" \
		M="$(PKG_BUILD_DIR)" \
		EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
		SoC=$(SOC) \
		$(KERNEL_MAKE_FLAGS) \
		$(PKG_JOBS) \
		modules
endef

$(eval $(call KernelPackage,qca-nss-drv))
